/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MaterialPack;

import Supporter.GlobalVariables;
import Supporter.ImageSupporter;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Van persie
 */
public class SubGroup extends MaterialObject {
    InfoPanel BlurLabel = null;
    InfoPanel ActiveLabel = null;
    
    Image background = null;
    Image orgBackground = null;
    private Vector Items = null;
    Vector Objects = null;
    
    int orgX,orgY;
    int tempWid,tempHei;
    boolean isRepair = false;
    int Icrment = 25;
    boolean isChanged = true;

    public SubGroup(Image plane, Image ActiveName,String name, int  x,int y, int wid,int hei )
    {
       
        orgX = xPos = x;       
        orgY = yPos = y;
        width = wid;
        height = hei;
        
        
        background = plane;
        orgBackground = Image.createImage(background);
        
        Items = new Vector();
        Objects = new Vector();
        
        Image temp = ImageSupporter.scaleImage(ActiveName, width/3, height/8);
        
        ActiveLabel = new InfoPanel(ImageSupporter.transparent(temp), name, x + 5   , y - 13 , width/3, 30, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC  ,Font.SIZE_LARGE));
        ActiveLabel.SetColorText(10, 10, 50);
        //ActiveLabel.SetAlphaBack();
        
        BlurLabel = new InfoPanel(ImageSupporter.transparent(ImageSupporter.alphaBlend(temp)), name, x + width/3 + 3,  y - 13 , width/3,30, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC  ,Font.SIZE_LARGE));
        BlurLabel.SetColorText(220, 180, 150);
        //BlurLabel.SetAlphaBack();
        isVisible = false;
    }
     public void Check()
             
             
             
    {
        
    }
    public boolean Press(int x,int y)
    {
        if(isVisible == false)
        {
            if(BlurLabel.isContained(x, y))
                isPressed = true;
            return false;
        }
        boolean ret  = false;
        for(int i = 0 ; i < Objects.size(); i++)       
        {
            
            if(((MaterialObject)Objects.elementAt(i)).Press(x,y))
            {
                
                ret = true;
            }
            
        }
        
        return ret;
    }
    public void Drag(int x, int y)
    {
        if(isVisible == false)
            return ;
        for(int i = 0 ; i < Objects.size(); i++)       
            ((MaterialObject)Objects.elementAt(i)).Drag(x, y);
    }
    public boolean Release(int x, int y)
    {
        if(isVisible == false)
        {
            if(BlurLabel.isContained(x, y))
            {
                isPressed = false;
                return true;
            }
            return false;
        }
        for(int i = 0 ; i < Objects.size(); i++)       
            ((MaterialObject)Objects.elementAt(i)).Release(x,y);
        return false;
    }
    public void Add(MaterialObject obj)
    {
        if(obj.getClass() == PetItem.class)
        {
            int price = Math.abs( GlobalVariables.myRandom.nextInt()%3 + 3);
            int kCal = 500 + price*100;
            int exp = 1 + price;
            ((PetItem)obj).SetATT(exp,price,kCal);
            getItems().addElement(obj);
        }
        Objects.addElement(obj);
    }
    public void ArrangeItem()
    {
        
        int counts = getItems().size();
        if(counts <= 0)
            return;
        int scalar = ((MaterialObject)getItems().elementAt(0)).getWidth() ;
        int wid = width/10;
        int hei = height/5;
        //int counts = getItems().size();
        int cols = (width-wid)/(wid + scalar);
        //int rows = counts/cols ;
        for(int i = 0; i  < counts; i++)     
        {
            if(getItems().elementAt(i).getClass() == PetItem.class)
            {
                ((MaterialObject)getItems().elementAt(i)).setxPos(orgX + 3*wid/4 + wid*(i%cols) + (i%cols)*scalar );
                ((MaterialObject)getItems().elementAt(i)).setyPos(orgY + hei/2 + hei*(i/cols) + (i/cols)*scalar + scalar/2 );
            }
        }
    }
    public boolean Expand(int anchor)
    {
        tempWid += Icrment;
        tempHei += Icrment;
        if(anchor == Graphics.LEFT)
        {
            setxPos(orgX + getWidth() - tempWid);
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        else
        {
            setxPos(orgX);
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        
        
        if( tempWid >= getWidth() || tempHei >= getHeight())
        {
            
            setxPos(orgX);
            setyPos(orgY);
            setIsVisible(true);
            background = Image.createImage(orgBackground);
            isChanged = true;
          
            return true;
        }
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        return false;
    }
    public boolean Shrink(int anchor)
    {           
        tempWid -= Icrment;
        tempHei -= Icrment;
        if(anchor == Graphics.RIGHT)
        {
            setxPos(orgX + getWidth() - tempWid);
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        else
        {
            setxPos(orgX);
            //xPos = orgX + width - tempWid;
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        
        if( tempWid <= 0 || tempHei <= 0)
        {
            setxPos(orgX);
            setyPos(orgY);
            background = Image.createImage(orgBackground);

            setIsVisible(false);
            isChanged = true;
            return true;
        }
        
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        return false;
    }
    public void setIsVisible(boolean  bit)
    {
        super.setIsVisible(bit);
        for(int i = 0 ; i < Objects.size(); i++)       
            ((MaterialObject)Objects.elementAt(i)).setIsVisible(bit);
    }
    
    public void Back()
    {
        //setIsVisible(false);
        isVisible = true;
        
        tempWid = getWidth();            
        tempHei = getHeight();
        isChanged = false;
    }
    public void Front()
    {
        //setIsVisible(false);
        isVisible = true;      
        
        tempWid = 0;
        tempHei = 0;
        isChanged = false;
    }
    public void paint(Graphics g)
    {
        if(isVisible == true)
        {
            
            g.drawImage(background, getxPos(), getyPos(),Graphics.TOP | Graphics.LEFT);
            
            if(isChanged)
            {
                  for(int i=0; i <Objects.size(); i++)
                    ((MaterialObject)Objects.elementAt(i)).paint(g);
                ActiveLabel.paint(g);
            }
        }   
        else 
            BlurLabel.paint(g);
    }

    /**
     * @return the Items
     */
    public Vector getItems() {
        return Items;
    }

    /**
     * @param Items the Items to set
     */
    public void setItems(Vector Items) {
        this.Items = Items;
    }
}
